package com.connector.server.util;

import org.slf4j.LoggerFactory;
import com.connector.base.connection.impl.SocketIoConnection;
import com.connector.base.connection.impl.NettyConnection;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.connector.base.connection.Connection;
import com.connector.base.connection.channel.SocketChannel;
import java.util.concurrent.ConcurrentMap;
import com.connector.base.protocol.Packet;
import java.util.Set;
import com.connector.server.jmx.JmxMessageBean;
import org.slf4j.Logger;

public class SendUtil
{
    private static final Logger log;

    public static void sendMsg(final JmxMessageBean messageBean, final Set<String> userIds, final Packet packet, final ConcurrentMap<SocketChannel, Connection> allConnection, final String message, final String sendUserId) {
        if (null != userIds && !userIds.isEmpty()) {
            final List<Connection> connections =  Lists.newArrayList();
            final List<String> onlionUserIds = Lists.newArrayList();
            for (final String userId : userIds) {
                boolean flag = false;
                for (final Connection c : allConnection.values()) {
                    final String uId = c.getSessionContext().getUserId();
                    if (userId.equals(uId)) {
                        connections.add(c);
                        flag = true;
                    }
                }
                if (flag) {
                    onlionUserIds.add(userId);
                }
            }
            final String messageId = messageBean.getOtherText();
            Boolean sendStatus = false;
            String noSendType = "";
            if (!Strings.isNullOrEmpty(messageId)) {
                sendStatus = true;
                if (messageBean.getPcOrApp()) {
                    noSendType = "app";
                }
                else {
                    noSendType = "pc";
                }
            }
            sendToConnection(connections, packet, message, sendStatus, sendUserId, noSendType);
            final Map<String, String> paramMap =  Maps.newHashMap();
            paramMap.put("message", JSON.toJSONString(messageBean));
            paramMap.put("onlionUserIds", StringUtils.join(onlionUserIds, ","));
            paramMap.put("userId", sendUserId);
            try {
                HttpUtil.postToInstantServer_addMessage(paramMap);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void sendToConnection(final List<Connection> connections, final Packet packet, final String message, final Boolean sendStatus, final String sendUserId, final String noSendType) {
        if (null != connections) {
            if (!connections.isEmpty()) {
                for (final Connection connection : connections) {
                    if (connection instanceof NettyConnection) {
                        final String userId = connection.getSessionContext().getUserId();
                        if (sendStatus && noSendType.equals("app")) {
                            if (!userId.equals(sendUserId)) {
                                ((NettyConnection)connection).setUserId(userId);
                                connection.channelSend((Object)packet);
                            }
                        }
                        else {
                            ((NettyConnection)connection).setUserId(userId);
                            connection.channelSend((Object)packet);
                        }
                    }
                    if (connection instanceof SocketIoConnection) {
                        if (sendStatus && noSendType.equals("pc")) {
                            final String userId = connection.getSessionContext().getUserId();
                            if (userId.equals(sendUserId)) {
                                continue;
                            }
                            connection.sendEvent("message", new Object[] { message });
                        }
                        else {
                            connection.sendEvent("message", new Object[] { message });
                        }
                    }
                }
            }
        }
    }

    static {
        log = LoggerFactory.getLogger((Class)SendUtil.class);
    }
}
